<template>
  <div>
    <template v-if="ishead">
      <template v-if="itemdata.lCanOutput === '0'">
        <input v-model="itemdata.value" style="display: none;">
      </template>
      <template v-else-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
        <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
        <van-field size="large" :id="itemdata.sField + '_ref'" required readonly placeholder="系统自动填充"
          v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
      </template>
      <template v-else-if="itemdata.lMustInput === '1'">
        <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
        <van-field size="large" v-if="itemdata.sUITypeExp == 'normal'" placeholder="请选择" :id="itemdata.sField + '_ref'"
          required clearable v-model="itemdata.value" :label="itemdata.sTitle" right-icon="search"
          @input="clearRefValue(itemdata)" @clear="clearRefValue(itemdata)"
          @change="openRef(itemdata, 'headdata', '', 0)" @click="openRef(itemdata, 'headdata', '', 0)"
          @click-right-icon="openRef(itemdata, 'headdata', '', 0)" :error-message="itemdata.errormsg" />
        <van-field size="large" v-else-if="itemdata.sUITypeExp == 'multi'" placeholder="请选择"
          :id="itemdata.sField + '_ref'" required clearable v-model="itemdata.value" :label="itemdata.sTitle"
          right-icon="search" @input="clearRefValue(itemdata)" @clear="clearRefValue(itemdata)"
          @click="openRef(itemdata, 'headdata', '', 0)" @click-right-icon="openRefMulti(itemdata, 'headdata', '', 0)"
          :error-message="itemdata.errormsg" />
      </template>
      <template v-else-if="itemdata.lCanInput === '0'">
        <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
        <van-field size="large" :id="itemdata.sField + '_ref'" placeholder="系统自动填充" readonly v-model="itemdata.value"
          :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
      </template>
      <template v-else>
        <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
        <van-field size="large" v-if="itemdata.sUITypeExp == 'normal'" placeholder="请选择" :id="itemdata.sField + '_ref'"
          clearable v-model="itemdata.value" :label="itemdata.sTitle" right-icon="search"
          @input="clearRefValue(itemdata)" @clear="clearRefValue(itemdata)"
          @change="openRef(itemdata, 'headdata', '', 0)" @click="openRef(itemdata, 'headdata', '', 0)"
          @click-right-icon="openRef(itemdata, 'headdata', '', 0)" :error-message="itemdata.errormsg" />
        <!-- <van-field size="large" v-else-if="itemdata.sUITypeExp == 'multi'" placeholder="请选择" :id="itemdata.sField + '_ref'" clearable v-model="itemdata.value" :label="itemdata.sTitle" right-icon="search" @input="clearRefValue(itemdata)" @clear="clearRefValue(itemdata)" @click="openRef(itemdata, 'bodydata', indexName, index)" @click-right-icon="openRefMulti(itemdata, 'headdata', '', 0)" :error-message="itemdata.errormsg" /> -->
        <van-field size="large" v-else-if="itemdata.sUITypeExp == 'multi'" placeholder="请选择"
          :id="itemdata.sField + '_ref'" clearable v-model="itemdata.value" :label="itemdata.sTitle" right-icon="search"
          @input="clearRefValue(itemdata)" @clear="clearRefValue(itemdata)"
          @click="openRefMulti(itemdata, 'headdata', '', 0)" :error-message="itemdata.errormsg" />
      </template>
    </template>
    <template v-else-if="itemdata.sUITypeExp == 'normal'">
      <template v-if="itemdata.lCanOutput === '0'">
        <input v-model="itemdata.value" style="display: none;">
      </template>
      <template v-else-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
        <input type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
        <van-field size="large" :id="index + '_' + itemdata.sField + '_ref'" required placeholder="系统自动填充" readonly
          v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
      </template>
      <template v-else-if="itemdata.lMustInput === '1'">
        <input type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
        <van-field size="large" placeholder="请选择" :id="index + '_' + itemdata.sField + '_ref'" required clearable
          v-model="itemdata.value" :label="itemdata.sTitle" right-icon="search" @input="clearRefValue(itemdata)"
          @clear="clearRefValue(itemdata)" @change="openRef(itemdata, 'bodydata', indexName, index)"
          @click="openRef(itemdata, 'bodydata', indexName, index)"
          @click-right-icon="openRef(itemdata, 'bodydata', indexName, index)" :error-message="itemdata.errormsg" />
      </template>
      <template v-else-if="itemdata.lCanInput === '0'">
        <input type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
        <van-field size="large" :id="index + '_' + itemdata.sField + '_ref'" placeholder="系统自动填充" readonly
          v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
      </template>
      <template v-else>
        <input type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
        <van-field size="large" placeholder="请选择" :id="index + '_' + itemdata.sField + '_ref'" clearable
          v-model="itemdata.value" :label="itemdata.sTitle" right-icon="search" @input="clearRefValue(itemdata)"
          @clear="clearRefValue(itemdata)" @change="openRef(itemdata, 'bodydata', indexName, index)"
          @click="openRef(itemdata, 'bodydata', indexName, index)"
          @click-right-icon="openRef(itemdata, 'bodydata', indexName, index)" :error-message="itemdata.errormsg" />
      </template>
    </template>
    <template v-else-if="itemdata.sUITypeExp == 'multi'">
      <template v-if="itemdata.lCanOutput === '0'">
        <input v-model="itemdata.value" style="display: none;">
      </template>
      <template v-else-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
        <input type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
        <van-field size="large" :id="index + '_' + itemdata.sField + '_ref'" required placeholder="系统自动填充" readonly
          v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
      </template>
      <template v-else-if="itemdata.lMustInput === '1'">
        <input type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
        <van-field size="large" placeholder="请选择" :id="index + '_' + itemdata.sField + '_ref'" required clearable
          v-model="itemdata.value" :label="itemdata.sTitle" right-icon="search" @input="clearRefValue(itemdata)"
          @clear="clearRefValue(itemdata)" @click="openRef(itemdata, 'bodydata', indexName, index)"
          @click-right-icon="openRefMulti(itemdata, 'bodydata', indexName, index)" :error-message="itemdata.errormsg" />
      </template>
      <template v-else-if="itemdata.lCanInput === '0'">
        <input type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
        <van-field size="large" :id="index + '_' + itemdata.sField + '_ref'" placeholder="系统自动填充" readonly
          v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
      </template>
      <template v-else>
        <input type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
        <van-field size="large" placeholder="请选择" :id="index + '_' + itemdata.sField + '_ref'" clearable
          v-model="itemdata.value" :label="itemdata.sTitle" right-icon="search" @input="clearRefValue(itemdata)"
          @clear="clearRefValue(itemdata)" @click="openRef(itemdata, 'bodydata', indexName, index)"
          @click-right-icon="openRefMulti(itemdata, 'bodydata', indexName, index)" :error-message="itemdata.errormsg" />
      </template>
    </template>
    <div style="margin-left: 12px;" v-if="lslevel.length > 0">
      已备案标签：<span class="lslevel" v-for="ls in lslevel" :key="ls">{{ ls }}</span>
    </div>
  </div>
</template>
<script>
import { Toast } from 'vant'
import Vue from 'vue'
import qs from 'qs'
import { base64encode } from '@/assets/js/tool.js'
Vue.use(Toast)
export default {
  data: function () {
    return {
      // clickmodel: {},
      // clickfrom: '',
      // clickindexName: '',
      // clickindex: 0
      lslevel: []
    }
  },
  // props: ['clickmodel', 'clickfrom', 'clickindexName', 'clickindex', 'jsondata', 'indexName', 'itemdata', 'ishead', 'index', 'activeNames'],
  props: {
    clickmodel: {
      type: Object,
      required: false
    },
    clickfrom: {
      type: String,
      required: false
    },
    clickindexName: {
      type: String,
      required: false
    },
    clickindex: {
      type: Number,
      required: false
    },
    jsondata: {
      type: Object,
      required: false
    },
    indexName: {
      type: String,
      required: false
    },
    itemdata: {
      type: Object,
      required: false
    },
    ishead: {
      type: Boolean,
      default: false
    },
    index: {
      type: Number,
      required: false
    }
  },
  watch: {
    'itemdata.value': {
      handler(newval) {
        let _this = this
        if (this.itemdata.idObject == 'accounts' && this.itemdata.sField == 'account') {
          this.axios({
            method: 'post',
            url: '/api/account.getLslevels.do',
            data: qs.stringify({
              aid: this.itemdata.valuehidden
              // aid: '17235286301461683220932211530867'
            })
          }).then(res => {
            //console.log('account.getLslevels.do', res)
            _this.lslevel = res.data.lslevel
          }).catch(res => {
            this.reserror(res)
          })
        }
        this.$emit('handleFiledLink', { sField: this.itemdata.sField, value: newval, ishead: this.ishead, index: this.index })
      }
    }
  },
  methods: {
    clearRefValue(datamodel) {
      if (!datamodel.value) {
        return // 如果输入框的值为空，不做任何操作
      }
      datamodel.value = '' // 清空输入框的值
      if (datamodel.value === '') {
        datamodel.valuehidden = ''
      }
    },
    openRef(datamodel, datafrom, indexName, index) {
      //console.log(datamodel.valuehidden + 'hidden')
      //console.log('datamodel', datamodel)
      this.$emit('update:clickmodel', datamodel)
      this.$emit('update:clickfrom', datafrom)
      this.$emit('update:clickindexName', indexName)
      this.$emit('update:clickindex', index)
      this.conditionValHidden = ''
      let inview = datamodel.inview
      if (inview !== '') {
        let arrinview = inview.split(';')
        for (let inviewindex in arrinview) {
          let arrinviewdata = arrinview[inviewindex]
          let inviewdatas = arrinviewdata.split(':')
          if (inviewdatas[1] === 'eq') {
            if (datafrom === 'headdata') {
              for (let groupindex in this.jsondata.head) {
                for (let itemindex in this.jsondata.head[groupindex].groupvalue) {
                  let item = this.jsondata.head[groupindex].groupvalue[itemindex]
                  if (item.sField === inviewdatas[2].split('_')[2]) {
                    this.conditionValHidden += inviewdatas[0] + ':eqref:' + item.valuehidden + ';'
                  }
                }
              }
            } else if (datafrom === 'bodydata') {
              for (let itemindex in this.jsondata.bodydata[indexName][index]) {
                let item = this.jsondata.bodydata[indexName][index][itemindex]
                if (item.sField === inviewdatas[0]) {
                  this.conditionValHidden += inviewdatas[0] + ':eqref:' + item.valuehidden + ';'
                }
              }
            }
          } else if (inviewdatas[1] === 'in') {
            //console.log('-------------' + datamodel.value)
            this.conditionValHidden += inviewdatas[0] + ':in:' + datamodel.value + ';'
          }
        }
      }
      let refparams = ''
      if (datamodel.idObject === 'fee' && datamodel.jsfunc === 'getFeebillFyzl()') { //  // 费用报销单表体费用类别
        let fyzl = ''
        for (let groupindex in this.jsondata.head) {
          for (let itemindex in this.jsondata.head[groupindex].groupvalue) {
            let item = this.jsondata.head[groupindex].groupvalue[itemindex]
            if (item.sField === 'fyzl') {
              fyzl = item.valuehidden
            }
          }
        }
        if (!fyzl) {
          Toast('请先选择表头的费用种类')
          return
        }
        let json = {
          'feebill_fyzl': fyzl
        }
        //console.log(JSON.stringify(json))
        refparams = base64encode(JSON.stringify(json))
      }
      // 每个逻辑需要单独写
      if (datamodel.idObject === 'sorders' && datamodel.jsfunc === 'getAccountForSorder()') {
        let agent = ''
        let consumer = ''
        let type = ''
        for (let groupindex in this.jsondata.head) {
          for (let itemindex in this.jsondata.head[groupindex].groupvalue) {
            let item = this.jsondata.head[groupindex].groupvalue[itemindex]
            if (item.sField === 'agent') {
              agent = item.valuehidden
            } else if (item.sField === 'consumer') {
              consumer = item.valuehidden
            } else if (item.sField === 'type') {
              type = item.valuehidden
            }
          }
        }
        if (consumer === '') {
          Toast('请先选择客户')
          return
        }
        if (type === '297ef43353a100de0153a1fdcaa60611') {
          if (agent !== '') {
            Toast('自营模式不能输入代理商')
            return
          }
        } else {
          if (agent === '') {
            Toast('请先选择代理商')
            return
          }
        }
      }

      //console.log('clickmodel-ref1', this.clickmodel, '2', datamodel, '3', this.conditionValHidden, '4', refparams)
      //console.log('datamodel', datamodel)
      /* this.$router.push({
        name: 'ref',
        params: {
          bo: datamodel.idJoinObject,
          tobo: datamodel.idObject,
          field: datamodel.sField,
          fieldref: datamodel.sFieldRef,
          viewid: datamodel.sRefKey,
          conditionVal: '',
          conditionValHidden: this.conditionValHidden,
          classname: datamodel.classname,
          params: refparams
        }
      }) */
      let componentName = 'ref'
      //console.log('componentName', componentName)
      if ((datamodel.idJoinObject === 'employee' || datamodel.idJoinObject === 'dep')) {
        componentName = 'orgRef'
      }
      this.$router.push({
        name: componentName,
        params: {
          stitle: this.itemdata.sTitle,
          bo: datamodel.idJoinObject,
          tobo: datamodel.idObject,
          field: datamodel.sField,
          fieldref: datamodel.sFieldRef,
          viewid: datamodel.sRefKey,
          conditionVal: '',
          conditionValHidden: '',
          classname: datamodel.classname,
          params: refparams,
          mode: 'single',
          datafrom: datafrom
        }
      })
    },
    openRefMulti(datamodel, datafrom, indexName, index) {
      this.$emit('update:clickmodel', datamodel)
      this.$emit('update:clickfrom', datafrom)
      this.$emit('update:clickindexName', indexName)
      this.$emit('update:clickindex', index)
      this.conditionValHidden = ''
      let inview = datamodel.inview
      if (inview !== '') {
        let arrinview = inview.split(';')
        for (let inviewindex in arrinview) {
          let arrinviewdata = arrinview[inviewindex]
          let inviewdatas = arrinviewdata.split(':')
          if (inviewdatas[1] === 'eq') {
            if (datafrom === 'headdata') {
              for (let groupindex in this.jsondata.head) {
                for (let itemindex in this.jsondata.head[groupindex].groupvalue) {
                  let item = this.jsondata.head[groupindex].groupvalue[itemindex]
                  if (item.sField === inviewdatas[2].split('_')[2]) {
                    this.conditionValHidden += inviewdatas[0] + ':eqref:' + item.valuehidden + ';'
                  }
                }
              }
            } else if (datafrom === 'bodydata') {
              for (let itemindex in this.jsondata.bodydata[indexName][index]) {
                let item = this.jsondata.bodydata[indexName][index][itemindex]
                if (item.sField === inviewdatas[0]) {
                  this.conditionValHidden += inviewdatas[0] + ':eqref:' + item.valuehidden + ';'
                }
              }
            }
          } else if (inviewdatas[1] === 'in') {
            this.conditionValHidden += inviewdatas[0] + ':in:;'
          }
        }
      }
      // 每个逻辑需要单独写
      if (datamodel.idObject === 'sorders' && datamodel.jsfunc === 'getAccountForSorder()') {
        let agent = ''
        let consumer = ''
        let type = ''
        for (let groupindex in this.jsondata.head) {
          for (let itemindex in this.jsondata.head[groupindex].groupvalue) {
            let item = this.jsondata.head[groupindex].groupvalue[itemindex]
            if (item.sField === 'agent') {
              agent = item.valuehidden
            } else if (item.sField === 'consumer') {
              consumer = item.valuehidden
            } else if (item.sField === 'type') {
              type = item.valuehidden
            }
          }
        }
        if (consumer === '') {
          Toast('请先选择客户')
          return
        }
        if (type === '297ef43353a100de0153a1fdcaa60611') {
          if (agent !== '') {
            Toast('自营模式不能输入代理商')
            return
          }
        } else {
          if (agent === '') {
            Toast('请先选择代理商')
            return
          }
        }
      }
      let refname = 'refmulti'
      if (datamodel.idJoinObject === 'employee') {
        refname = 'idownerMulti'
      }

      if (datamodel.idJoinObject == 'employee' || datamodel.idJoinObject === 'dep') {
        refname = 'orgRef'
      }
      this.$router.push({
        name: refname,
        params: {
          bo: datamodel.idJoinObject,
          tobo: datamodel.idObject,
          field: datamodel.sField,
          fieldref: datamodel.sFieldRef,
          viewid: datamodel.sRefKey,
          conditionVal: '',
          conditionValHidden: this.conditionValHidden,
          classname: datamodel.classname,
          valuehidden1: datamodel.valuehidden,
          datamodel1: datamodel
        }
      })
    }
  }
}
</script>
<style>
.lslevel {
  border: 1px solid gray;
  padding: 0px 3px;
  margin: 3px;
  display: inline-block;
}
</style>
